<?php
namespace App\Service\Admin\Qudao;

use App\Service\Admin\BaseService as Service;

class BannerService extends Service
{
    //轮播图

    protected $types = [
        1 => '渠道端首页', '商城发现页',
    ];

    /**
     * 列表
     */
    public function getList()
    {
        $db = $this->dbConn($this->dbQdBanner)->where('del', 0);
        $total = $db->count();
        $res = $db->get();
        if (!count($res)) {
            return $this->toback(-2, '没有记录');
        }
        $types = $this->types;
        $arys = [];
        foreach ($res as $v) {
            $v = (array) $v;
            $tname = array_key_exists($v['type'], $types) ? $types[$v['type']] : '-';
            $arys[] = [
                'id' => $v['id'],
                'type' => $v['type'],
                'tname' => $tname,
                'link' => $v['link'],
                'addtime' => $v['addtime'],
            ];
        }
        return $this->toback(0, '', $arys, $total);
    }

    /**
     * 添加
     * @param array $pas
     */
    public function add($pas, $w, $h)
    {
        $lastid = $this->dbConn($this->dbQdBanner)->insertGetId([
            'width' => $w, 'height' => $h,
            'type' => $pas['type'], 'link' => $pas['img'],
            'addtime' => toDate(),
        ]);
        if (!$lastid) {
            return $this->toback(-1, '添加失败');
        }
        return $this->toback();
    }

    /**
     * 删除
     * @param int $id
     */
    public function setDel($id)
    {
        $up = $this->dbConn($this->dbQdBanner)->where('id', $id)
            ->update(['del' => 1, 'deltime' => toDate()]);
        if (!$up) {
            return $this->toback(-1, '删除失败');
        }
        return $this->toback();
    }
}